// pages/classic/classic.js
import {classicModel} from "../../models/classic.js";
import {LikeModel} from "../../models/like.js";

const classic = new classicModel()
const likeModel = new LikeModel()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        classicData: {},
        first: false,
        latest: true,
        likeCount: 0,
        likeStatus: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        classic.getlatest(res => {
            console.log(res)
            this.setData({
                classicData: res,
                likeCount: res.fav_nums,
                likeStatus: res.like_status
            })
        })
    },

    onlive(e) {
        let state = e.detail.state
        let id = this.data.classicData.id
        let type = this.data.classicData.type
        likeModel.live(state, id, type)
    },

    onPageUp() {
        this._getclassicData('next')
    },
    onPageDown() {
        this._getclassicData('previous')
    },

    _getclassicData(nextOrprevious) {
        console.log(nextOrprevious)
        let index = this.data.classicData.index
        classic.getclassic(index, nextOrprevious, res => {
            this._getLiekStatus(res.id, res.type)
            this.setData({
                classicData: res,
                first: classic.isFirst(res.index),
                latest: classic.isLatest(res.index)
            })
            console.log(res)
        })
    },

    _getLiekStatus(artID, categoy) {
        likeModel.getClassicLikeStatus(artID, categoy, res => {
            this.setData({
                likeCount: res.fav_nums,
                likeStatus: res.like_status
            })
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})